//
//  CXHUBiMAXSplashAdapter.m
//  CXHAdSDK-UBiMAX
//
//  Created by 陈坤 on 2025/7/28.
//

#import "CXHUBiMAXSplashAdapter.h"
#import <UBiMAXAdSDK/UBiMAXAdSDK-umbrella.h>
#import <CXHAdSDK-Core/CXHAdSDKSplashAd.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>


@interface CXHUBiMAXSplashAdapter ()<UMTCustomSplashAdapter,CXHAdSDKSplashAdDelegate>

@property (nonatomic, strong) CXHAdSDKSplashAd *splash;
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, assign) UMTAdBidType umtBidType;
@property (nonatomic, assign) UMTVideoMuteType umtVideoMuteType;

@end

@implementation CXHUBiMAXSplashAdapter

/// 加载开屏广告
/// @param slotID 广告位ID
/// @param parameter 广告加载参数
- (void)loadSplashAdWithSlotID:(NSString *)slotID andParameter:(NSDictionary *)parameter {
    if (self.splash) {
        self.splash.delegate = nil;
        self.splash = nil;
    }
    NSNumber *bidType = parameter[UMTAdLoadingParamBiddingType];
    self.umtBidType = (UMTAdBidType)bidType.integerValue;
    
    UIView *bottomView = parameter[UMTAdLoadingParamSPCustomBottomView];
    self.bottomView = bottomView;
    
    self.splash = [[CXHAdSDKSplashAd alloc] init];
    self.splash.posId = slotID;
    self.splash.delegate = self;
    self.splash.bottomView = bottomView;
    [self.splash loadSplashAd];
   
}

/// 展示开屏广告
/// @param window 广告展示窗口
/// @param parameter 广告展示参数
- (void)showSplashAdInWindow:(UIWindow *)window parameter:(NSDictionary *)parameter {
    [self.splash showSplashAdInWindow:window];
    self.splash.controller = window.rootViewController;
    // 模拟广告展示回调
//    [self.bridge splashAdWillVisible:self];
}

/// 在适配器被释放前
- (void)destroyAd {
//    [self removeSplash];
}

/// 当前加载的广告的状态
- (UMTMediatedAdStatus)mediatedAdStatus {
    // 调用广告源提供的广告有效性校验方法
    if (self.splash && self.splash.isAdValid) {
        // 广告有效
        return UMTMediatedAdStatusNormal;
    }
    return UMTMediatedAdStatusUnknown;
}

- (void)didReceiveBidResult:(UMTMediaBidResult *)result {
    // 在此处理Client Bidding的结果回调
    if (result.win) {
        NSInteger winPrice = self.splash.getBidPrice;
        if (result.winnerPrice) {
            winPrice = result.winnerPrice;
        }
        [self.splash sendWinNotice:winPrice];
    } else {
        if (result.winnerPrice) {
            [self.splash sendLossNotice:result.winnerPrice];
        }
    }
}

#pragma mark - CXHAdSDKSplashAdDelegate

/**
 开屏广告点击回调, 可能会打开 AppStore, WebView, ThirdApp etc.

 @param splashAd 开屏广告
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_splashAdDidClick:(CXHAdSDKSplashAd *)splashAd loadingPageURL:(NSString *)loadingPageURL{
    
    [self.bridge splashAdDidClick:self];
}

/**
 开屏广告关闭回调, 触发条件: 点击广告, 点击跳过, 超时关闭

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidClose:(CXHAdSDKSplashAd *)splashAd{
    
    [self.bridge splashAdDidClose:self];
}

/**
 开屏广告展示回调

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdExposured:(CXHAdSDKSplashAd *)splashAd{
    [self.bridge splashAdDidShow:self];
}

/**
 开屏广告加载成功
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidLoad:(CXHAdSDKSplashAd *)splashAd{
    NSLog(@"%s", __func__);
    NSMutableDictionary *extDic = [NSMutableDictionary dictionary];
    if (self.umtBidType == UMTAdBidType_ClientBidding) {
        NSInteger price = [splashAd getBidPrice];
        if (price>0) {
            [extDic setValue:[NSString stringWithFormat:@"%ld", price] forKey:UMTAdnAdLoadedExtECPM];
        }
    }
    [self.bridge splashAd:self didLoadWithExt:extDic];
    [self.bridge splashAd:self didRenderSuccessWithExt:extDic];
}

/**
 开屏广告加载失败
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidFailToLoad:(CXHAdSDKSplashAd *)splashAd error:(NSError *)error{
    
    [self.bridge splashAd:self didLoadFailWithError:error ext:@{}];
}

/**
 *  开屏广告成功展示
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdSuccessPresentScreen:(CXHAdSDKSplashAd *)splashAd{
    
}

/**
 *  开屏广告展示失败
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdFailToPresent:(CXHAdSDKSplashAd *)splashAd withError:(NSError *)error{
    [self.bridge splashAdDidShowFailed:self error:error];
}

/**
 *  开屏广告关闭落地页
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdCloseLandingPage:(CXHAdSDKSplashAd *)splashAd{
}


@end
